/*
 * Copyright 2010-2011 Sanjay Dwivedi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.isdinvestments.cam.application;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.isdinvestments.cam.domain.model.Account;
import com.isdinvestments.cam.domain.model.Customer;
import com.isdinvestments.cam.domain.model.SubAccount;
import com.isdinvestments.cam.infrastructure.repository.CustomerRepository;
import com.isdinvestments.cam.infrastructure.repository.CustomerRepository;
import com.isdinvestments.common.validation.ConstraintViolationException;

@Service("customerService")
@Repository
@Transactional(propagation = Propagation.REQUIRED, 
		isolation = Isolation.DEFAULT, 
		readOnly = false)
public class CustomerServiceImpl implements CustomerService {
	
	@Autowired
	private CustomerRepository customerRepository;
	@Autowired
	private Validator validator;

	/**
	 * @throws ConstraintViolationException to indicate model validation errors.
	 */
	@Override
	@Transactional(readOnly = false)
	public Customer createCustomer(Customer customer)
	{
		// Validation
		if(customer == null) {
			throw new IllegalArgumentException();
		} 
		Set<? extends ConstraintViolation<?>> violations = validator.validate(customer);
		if (violations != null && violations.size() > 0) {
				throw new ConstraintViolationException(violations);
		}
		
		// Business rules/logic/processing
		
		// Persist
		return this.customerRepository.save(customer);
	}
	
	@Override
	public Customer getCustomer(Long customerId) {
		// Validation
		if(customerId == null) {
			throw new IllegalArgumentException();
		}
		
		// get customer
		return this.customerRepository.findOne(customerId);
	}
	
	@Override
	public List<Customer> getAllCustomers() {
		return this.customerRepository.findAll();
	}

	@Override
	@Transactional(readOnly = false)
	public void updateCustomer(Customer customer) {
		if(customer == null) {
			throw new IllegalArgumentException();
		} 
		Set<? extends ConstraintViolation<?>> violations = validator.validate(customer);
		if (violations != null && violations.size() > 0) {
				throw new ConstraintViolationException(violations);
		}
		
		this.customerRepository.saveAndFlush(customer);
	}
	
/*	@Override
	@Transactional(readOnly = false)
	public void closeAccount(Long accountId) {

	}

	@Override
	@Transactional(readOnly = false)
	public void closeSubAccount(Long subAccountId) {

	}

	@Override
	public List<Account> getAllAccounts() {
		return null;
	}

	@Override
	public List<SubAccount> getAllSubAccounts() {
		return null;
	}

	@Override
	@Transactional(readOnly = false)
	public void createAccount(Account account) {
		// Validation
		if(account == null) {
			throw new IllegalArgumentException();
		}  
		
		// Business rules/logic/processing
		
		// Persist

	}

	@Override
	@Transactional(readOnly = false)
	public void createSubAccount(Long accountId, SubAccount subAccount) {
		

	}

	@Override
	@Transactional(readOnly = false)
	public void updateAccount(Account account) {

	}

	@Override
	@Transactional(readOnly = false)
	public void updateAccount(SubAccount subAccount) {

	}
*/
}
